﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace TraditionalMalteseCuisine.App_Code.Objects
{
    [Serializable]
    public class SiteNews : BaseTable
    {
        //*************************************************
        //                Class Members
        //*************************************************
        #region Class Members
        public const string TABLE_NAME = "SiteNews";

        public const string NEWS_TEXT_TAG = "news_text";
        public const string POSTED_DATE_TAG = "posted_date";

        private string mNewsText = string.Empty;
        private DateTime mPostedDate = DateTime.MinValue;
        #endregion
        //*************************************************


        //*************************************************
        //                 Constructor
        //*************************************************
        #region Constructor
        public SiteNews()
        {

        }
        #endregion
        //*************************************************


        //*************************************************
        //               Static Methods
        //*************************************************
        #region Static Methods
        public static OleDbCommand GetCommand_GetLatestNews(int amount)
        {
            OleDbCommand oledbCommand = new OleDbCommand();
            oledbCommand.CommandType = CommandType.Text;

            StringBuilder commandText = new StringBuilder();
            commandText.AppendFormat("SELECT TOP {0} * FROM {1} ORDER BY {2} DESC", amount, TABLE_NAME, POSTED_DATE_TAG);

            oledbCommand.CommandText = commandText.ToString();

            return oledbCommand;
        }
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        protected override string GetInsertScriptUsingParameters()
        {
            StringBuilder insertScript = new StringBuilder();

            insertScript.Append("INSERT INTO ");
            insertScript.AppendFormat("{0} ", TABLE_NAME);
            insertScript.AppendFormat("([{0}]) ", NEWS_TEXT_TAG);
            insertScript.Append("VALUES ");
            insertScript.AppendFormat("(@{0}) ", NEWS_TEXT_TAG);

            return insertScript.ToString();
        }

        public override OleDbCommand GetInsertCommand()
        {
            OleDbCommand insertCommand = new OleDbCommand();

            insertCommand.CommandType = CommandType.Text;
            insertCommand.CommandText = GetInsertScriptUsingParameters();

            insertCommand.Parameters.AddWithValue(string.Format("@{0}", NEWS_TEXT_TAG), NewsText);

            return insertCommand;
        }

        public override BaseTable ReadDataRow(DataRow datarow)
        {
            ID = int.Parse(datarow[ID_TAG].ToString());
            NewsText = datarow[NEWS_TEXT_TAG].ToString();
            PostedDate = DateTime.Parse(datarow[POSTED_DATE_TAG].ToString());
            InUse = bool.Parse(datarow[IN_USE_TAG].ToString());

            return this;
        }


        public override string ToString()
        {
            return string.Format("{0} - {1}", NewsText, PostedDate);
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public string NewsText
        {
            get { return mNewsText; }
            set { mNewsText = value; }
        }

        public DateTime PostedDate
        {
            get { return mPostedDate; }
            set { mPostedDate = value; }
        }
        #endregion
        //*************************************************
    }
}